<template>
	<!-- 性别选取使用 单项选择器
	 ，内部由多个 <radio> 组成。通过把多个radio包裹在一个radio-group下，实现这些radio的单选。
	 -->
	<view class="page page-fill">

		<form @submit="formSubmitSex">

			<view class="page-block" style="margin-top: 20upx;">

				<radio-group class="radio-sex" @change="sexChange">
					<!-- 等于1的时候才是true -->
					<label class="radio-single">
						<radio value="1" :checked="sex == 1" />男
					</label>
					<label class="radio-single">
						<radio value="0" :checked="sex == 0" />女
					</label>
				</radio-group>

			</view>

			<button type="primary" form-type="submit" class="submitBtn">提交</button>

		</form>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				globalUser: {},
				sex: "-1"
			}
		},
		onLoad() {
			var me = this;
			var globalUser = me.getGlobalUser("globalUser");
			me.globalUser = globalUser;
			me.sex = globalUser.sex;
		},
		methods: {
			//获取选中的值 1 / 0 
			sexChange(e) {
				this.sex = e.detail.value;
			},
			//套路还是 获取输入的值准备好请求参数 发送请求 获取到数据 存储本地 跳转页面 之否则提示用户错误信息
			formSubmitSex() {
				var me = this;
				var sex = me.sex;

				uni.request({
					url: me.serverUrl + "/user/modifyUserinfo",
					data: {
						"userId": me.globalUser.id,
						"sex": sex
					},
					header: {
						"headerUserId": me.globalUser.id,
						"headerUserToken": me.globalUser.userUniqueToken
					},
					method: "POST",
					success(res) {
						var resData = res.data;
						// console.log(typeof(resData));
						if (resData.status == 200) {
							// 获得最新的用户数据
							var userInfo = resData.data;
							uni.setStorageSync("globalUser", userInfo);
							uni.navigateBack({
								delta: 1
							})
						} else if (resData.status == 502 || resData.status == 500) {
							uni.showToast({
								title: res.data.msg,
								image: "../../static/icos/error.png",
								duration: 2000
							})
						}
					}
				})

			}
		}
	}
</script>

<style>

</style>
